//
//  HWOpenServiceInfoProtocol.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/29.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWOpenServiceInfoProtocol: NSObject {
    var models:[HWOpenServiceModel]?
    var didClickGame:(()->())?
    var didSelectImage:((_:Int,_:[String])->())?
    var images:[String]?
}

extension HWOpenServiceInfoProtocol:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        didClickGame?()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let gameCell = HWOpenServiceGameCell.cellWithTableView(tableView: tableView)
        gameCell.model = models?[indexPath.row]
        gameCell.collectionProtocol.didSelectImage = {
            index,imags in
            self.didSelectImage?(index,imags)
        }
        return gameCell
    }
    
}


extension HWOpenServiceInfoProtocol:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        didClickGame?()
        didSelectImage?(indexPath.row,images ?? [])
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        return UIEdgeInsets(top: 0, left: 20*widthScale, bottom: 0, right: 20*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 94*widthScale, height: 136*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let imageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWOpenServiceInfoImageCell", for: indexPath) as! HWOpenServiceInfoImageCell
        imageCell.imageView.sd_setImage(with: URL(string: images?[indexPath.row] ?? ""), completed: nil)
        return imageCell
    }
}


